package com.zhiyou.service.SystemLesseeServiceImpl;

import com.zhiyou.bean.Lessee;
import com.zhiyou.bean.LesseeExample;
import com.zhiyou.bean.Logistics;
import com.zhiyou.dao.LesseeMapper;
import com.zhiyou.service.SystemLesseeService.LesseeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class LesseeServiceImpl implements LesseeService {
    @Autowired
    LesseeMapper lesseeMapper;

    @Override
    public List<Lessee> getAll() {
        LesseeExample le = new LesseeExample();
        LesseeExample.Criteria criteria = le.createCriteria();
        criteria.andStateTo();
        List<Lessee> lessees = lesseeMapper.selectByExample(le);
        return lessees;
    }

    @Override
    public Lessee detail(int id) {
        Lessee lessee = lesseeMapper.selectByPrimaryKey(id);
        return lessee;
    }

    @Override
    public int update(Lessee lessee) {
        int i = lesseeMapper.updateByPrimaryKeySelective(lessee);
        return i;
    }

    @Override
    public int deleteOne(int id) {
        Lessee lessee = lesseeMapper.selectByPrimaryKey(id);
        lessee.setlState(1);
        int i = lesseeMapper.updateByPrimaryKey(lessee);
        return i;
    }

    @Override
    public int addOne(Lessee lessee) {
        lessee.setlState(0);
        lessee.setlAddtime(new Date());
        int i = lesseeMapper.insert(lessee);
        return i;
    }

    @Override
    public List<Lessee> search(String field, String keyword) {
        String str = "%" + keyword + "%";
        System.out.println("========111========");
        System.out.println("field = " + field + ",keyword = " + keyword);
        LesseeExample ex = new LesseeExample();
        LesseeExample.Criteria criteria = ex.createCriteria();
        criteria.andStateTo();
        List<Lessee> lessees = null;
        if ("lName".equals(field)){
            criteria.andLNameLike(str);
            lessees = lesseeMapper.selectByExample(ex);
        }else if ("lTel".equals(field)){
            System.out.println("--------lTel-------");
            criteria.andLTelLike(str);
            lessees = lesseeMapper.selectByExample(ex);
            System.out.println(lessees.size());
        }else if ("lSex".equals(field)){
            criteria.andLSexLike(str);
            lessees = lesseeMapper.selectByExample(ex);
        }else if ("lNativeplace".equals(field)){
            criteria.andLNativeplaceLike(str);
            lessees = lesseeMapper.selectByExample(ex);
        }else if ("lIdcard".equals(field)){
            criteria.andLIdcardLike(str);
            lessees = lesseeMapper.selectByExample(ex);
        }
        return lessees;
    }
}
